package com.skyworth.web.common;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author holin
 * @date 1/5/16.
 */
public class Util {
    public static String pathFromDate(Date date) {
        String newstring = new SimpleDateFormat("yyyy-MM-dd").format(date);
        return newstring;
    }

    public static int randInt(int min, int max) {
        return min + (int)(Math.random() * ((max - min) + 1));
    }

    public static String datetimeStr() {
        Date curDate = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(curDate);
    }

    public static String thedayStr() {
        Date curDate = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        return format.format(curDate);
    }

    public static boolean makeDir(String dir) {
        File theDir = new File(dir);
        boolean result = false;

        if (!theDir.exists()) {
            try{
                theDir.mkdir();
                result = true;
            }
            catch(SecurityException se){
                //handle it
            }
        }
        return result;
    }

    public static Map<String, Object> responseWith(Boolean success,
                                                   String message,
                                                   Object data) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("success", success);
        map.put("message", message);
        map.put("data", data);
        return map;
    }


    public static long getDateDiff(Date date1, Date date2, TimeUnit timeUnit) {
        long diffInMillies = date2.getTime() - date1.getTime();
        return timeUnit.convert(diffInMillies, TimeUnit.MILLISECONDS);
    }

    //---------private-----
    private static double rad(double d)
    {
        return d * Math.PI / 180.0;
    }
}
